package com.ymatou.iapi.auth.testcase;

import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.auth.parameter.SignAuthBean;
import com.ymatou.iapi.auth.parameter.UserFindBean;
import com.ymatou.iapi.auth.service.SignAuthCall;
import com.ymatou.iapi.auth.service.UserFindCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.core.tool.RSAUtils;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;

import com.ymt.utils.tag.P3;
import com.ymttest.business.service.AuthCallService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.sqlwapper.LoginHistoryWapper;
import com.ymttest.database.sqlwapper.UserSafeWapper;
import com.ymttest.database.sqlwapper.UserSafeWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
 * token认证 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_UserFind {
	private static UserFindBean ufb;
	private static UserFindCall ufc;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询用户信息");
	}

	@Before
	public void caseUp() {
		ufb = new UserFindBean();
		ufc = new UserFindCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(ufc.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	@Ignore 
	//查询mongo,不兼容java 废弃
	public void Tc_UserFind_001() {
		Logger.start(true,"find查询成功,验证数据记录");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String AccessToken=registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			String ip=regbean.getClientIp();
			Thread.sleep(3000);
			Logger.comment("Token查询开始");
			ufb.setClientIp(ip);
			ufb.setUserId(userId);
			ufc.setData(FormatUtil.beanToHttpString(ufb, true, false));
			ufc.callService();
			Logger.verifyEquals(true, ufc.getSuccess(), "验证Success");
			Map<String, String> result = ufc.getResult();
			Logger.verifyEquals(regbean.getLoginMail().toLowerCase(), result.get("LoginMail").toLowerCase(), "验证loginEmail");
			Logger.verifyEquals(regbean.getLoginId().toLowerCase(), result.get("LoginId").toLowerCase(), "验证loginId");
			Logger.verifyEquals(userId.toString(), result.get("UserId"), "验证userId");
			Logger.verifyEquals(regbean.getLoginMobile(), result.get("LoginMobile"), "验证loginMobile");
			Logger.verifyNotEquals("null",result.get("TPas"), "验证TPas");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	@Ignore 
	public void Tc_UserFind_002() {
		Logger.start(false,"没有userid，失败");
		try {
			Logger.comment("Token查询开始");
			ufb.setClientIp(TestDataManager.getRandomIp());
//			ufb.setUserId(UserId);
			ufc.setData(FormatUtil.beanToHttpString(ufb, true, false));
			ufc.callService();
			Logger.verifyEquals(false, ufc.getSuccess(), "验证Success");
			Logger.verifyEquals("404",ufc.getString("Code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	@Ignore 
	public void Tc_UserFind_003() {
		Logger.start(true,"密码正确find查询成功,验证数据记录");
		try {
			com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer UserId = uc.getUserid();
			String AccessToken=uc.getAccessToken();
			
			String UserName = urb.getLoginMobile();
			String ip=urb.getClientIp();
//			AuthCallService.authLogin(UserName, "123456a", "app", ip);
			Thread.sleep(3000);
			Logger.comment("Token查询开始");
			ufb.setClientIp(ip);
			ufb.setUserId(UserId);
			ufb.setLoginPassword("123456a");
			ufc.setData(FormatUtil.beanToHttpString(ufb, true, false));
			ufc.callService();
			Logger.verifyEquals(true, ufc.getSuccess(), "验证Success");
			Map<String, String> result = ufc.getResult();
			Logger.verifyEquals(urb.getLoginMail(), result.get("LoginMail").toLowerCase(), "验证loginEmail");
			Logger.verifyEquals(urb.getLoginId().toLowerCase(), result.get("LoginId").toLowerCase(), "验证loginId");
			Logger.verifyEquals(UserId.toString(), result.get("UserId"), "验证userId");
			Logger.verifyEquals(urb.getLoginMobile(), result.get("LoginMobile"), "验证loginMobile");
			Logger.verifyNotEquals("null",result.get("TPas"), "验证TPas");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	@Ignore 
	public void Tc_UserFind_004() {
		Logger.start(true,"密码不正确find查询成功,验证数据记录");
		try {
			com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer UserId = uc.getUserid();
			String AccessToken=uc.getAccessToken();
			
			String UserName = urb.getLoginMobile();
			String ip=urb.getClientIp();
//			AuthCallService.authLogin(UserName, "123456a", "app", ip);
			Thread.sleep(3000);
			Logger.comment("Token查询开始");
			ufb.setClientIp(ip);
			ufb.setUserId(UserId);
			ufb.setLoginPassword("111111");
			ufc.setData(FormatUtil.beanToHttpString(ufb, true, false));
			ufc.callService();
			Logger.verifyEquals(false, ufc.getSuccess(), "验证Success");
			Logger.verifyEquals("lpas error",ufc.getString("LastErrorMessage"), "验证LastErrorMessage");
			Logger.verifyEquals("400",ufc.getString("Code"), "验证Code");
			//{"Result":{"LoginId":null,"UserId":0,"LoginMail":null,"LoginMobile":null,"TPas":null},"Success":false,"LastErrorMessage":"lpas error","Code":400}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
